package com.yc.iot.scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yc.iot.db.User;
import com.yc.iot.db.UserRepository;
import com.yc.iot.mq.MqttGateway;

@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
	@Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;
	
	@Autowired
	private MqttGateway MqttGatewaySend;
	/**
	 * 数据库测试存储
	 * 
	 * 
	 * 
	 */
    @Scheduled(fixedRate = 50000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));

        User n = new User();
		n.setName("none");
		n.setEmail("66600987@163.com");
		userRepository.save(n);
    }
    /**
     * mqtt 测试发送
     * 
     * 
     */
    @Scheduled(fixedRate = 2000)
    public void mqttSendData() {
        log.info("mqtt has been send out");
        MqttGatewaySend.sendToMqtt("ssssssssssssssssssss");
    }    
}
